package com.strange.mdl.module.mdl.service.videoinfo;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.strange.mdl.module.mdl.controller.admin.videoinfo.vo.*;
import com.strange.mdl.module.mdl.dal.dataobject.videoinfo.MdlVideoInfoDO;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.pojo.PageParam;
import com.strange.mdl.framework.common.util.object.BeanUtils;

import com.strange.mdl.module.mdl.dal.mysql.videoinfo.MdlVideoInfoMapper;

import static com.strange.mdl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.strange.mdl.framework.common.util.collection.CollectionUtils.convertList;
import static com.strange.mdl.framework.common.util.collection.CollectionUtils.diffList;
import static com.strange.mdl.module.mdl.enums.ErrorCodeConstants.*;

/**
 * 视频信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MdlVideoInfoServiceImpl implements MdlVideoInfoService {

    @Resource
    private MdlVideoInfoMapper videoInfoMapper;

    @Override
    public Long createVideoInfo(MdlVideoInfoSaveReqVO createReqVO) {
        // 插入
        MdlVideoInfoDO videoInfo = BeanUtils.toBean(createReqVO, MdlVideoInfoDO.class);
        videoInfoMapper.insert(videoInfo);
        // 返回
        return videoInfo.getId();
    }

    @Override
    public void updateVideoInfo(MdlVideoInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateVideoInfoExists(updateReqVO.getId());
        // 更新
        MdlVideoInfoDO updateObj = BeanUtils.toBean(updateReqVO, MdlVideoInfoDO.class);
        videoInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteVideoInfo(Long id) {
        // 校验存在
        validateVideoInfoExists(id);
        // 删除
        videoInfoMapper.deleteById(id);
    }

    @Override
        public void deleteVideoInfoListByIds(List<Long> ids) {
        // 校验存在
        validateVideoInfoExists(ids);
        // 删除
        videoInfoMapper.deleteByIds(ids);
        }

    private void validateVideoInfoExists(List<Long> ids) {
        List<MdlVideoInfoDO> list = videoInfoMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(VIDEO_INFO_NOT_EXISTS);
        }
    }

    private void validateVideoInfoExists(Long id) {
        if (videoInfoMapper.selectById(id) == null) {
            throw exception(VIDEO_INFO_NOT_EXISTS);
        }
    }

    @Override
    public MdlVideoInfoDO getVideoInfo(Long id) {
        return videoInfoMapper.selectById(id);
    }

    @Override
    public PageResult<MdlVideoInfoDO> getVideoInfoPage(MdlVideoInfoPageReqVO pageReqVO) {
        return videoInfoMapper.selectPage(pageReqVO);
    }

}